/**
 * Copyright &copy; 2012-2016 <a href="https://git.oschina.net/zdw2016/webbase5">Webbase</a> All rights reserved.
 */
package com.foreveross.webbase.projectdemo.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.foreveross.webbase.common.persistence.Page;
import com.foreveross.webbase.common.service.CrudService;
import com.foreveross.webbase.common.utils.StringUtils;
import com.foreveross.webbase.projectdemo.dao.ServerDao;
import com.foreveross.webbase.projectdemo.entity.Server;
import com.foreveross.webbase.projectdemo.entity.ServerServertype;
import com.foreveross.webbase.projectdemo.entity.Servertype;
import com.foreveross.webbase.projectdemo.service.ServerServertypeService;
import com.foreveross.webbase.projectdemo.service.ServerService;
import com.foreveross.webbase.projectdemo.service.ServertypeService;
import com.google.common.collect.Lists;

/**
 * 服务器管理Service
 * 
 * @author yangwanli
 * @version 2017-03-16
 */
@Service
public class ServerServiceImpl extends CrudService<ServerDao, Server> implements ServerService {

	@Autowired
	private ServerServertypeService serverServertypeService;

	@Autowired
	private ServertypeService servertypeService;

	/**
	 * 保存数据（插入或更新）
	 * 
	 * @param entity
	 */
	@Override
	@Transactional(readOnly = false)
	public void save(Server server) {
		if (server.getIsNewRecord()) {
			server.preInsert();
			server.setCreattime(new Date());
			dao.insert(server);
		} else {
			server.preUpdate();
			dao.update(server);
		}
		serverServertypeService.deleteServerId(server.getId());
		String[] strs = server.getServertype().split(",");
		for (String str : strs) {
			ServerServertype serverServertype = new ServerServertype();
			serverServertype.setTypeid(str);
			serverServertype.setServerid(server.getId());
			serverServertypeService.save(serverServertype);
		}
	}

	/**
	 * 获取单条数据
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public Server get(String id) {
		Server server = dao.get(id);

		// 去中间表查询对应的tyepid
		ServerServertype serverServertype = new ServerServertype();
		serverServertype.setServerid(server.getId());
		List<ServerServertype> serverServertypeList = serverServertypeService.findList(serverServertype);
		// 用于拼接服务器类型type
		String type = "";
		for (int i = 0; i < serverServertypeList.size(); i++) {
			// 根据重中间表获取的typeid查询服务器类型type
			Servertype servertype = servertypeService.get(serverServertypeList.get(i).getTypeid());
			// 拼接
			type += servertype.getId();
			// 最后一个不加逗号
			if (i != serverServertypeList.size() - 1) {
				type += ",";
			}
		}
		// 将type赋值给servertype
		server.setServertype(type);

		return server;
	}

	/**
	 * 获取单条数据
	 * 
	 * @param entity
	 * @return
	 */
	@Override
	public Server get(Server server) {
		server = dao.get(server);
		// 去中间表查询对应的tyepid
		ServerServertype serverServertype = new ServerServertype();
		serverServertype.setServerid(server.getId());
		List<ServerServertype> serverServertypeList = serverServertypeService.findList(serverServertype);
		// 用于拼接服务器类型type
		String type = "";
		for (int i = 0; i < serverServertypeList.size(); i++) {
			// 根据重中间表获取的typeid查询服务器类型type
			Servertype servertype = servertypeService.get(serverServertypeList.get(i).getTypeid());
			// 拼接
			type += servertype.getId();
			// 最后一个不加逗号
			if (i != serverServertypeList.size() - 1) {
				type += ",";
			}
		}
		// 将type赋值给servertype
		server.setServertype(type);
		return server;
	}

	/**
	 * 查询列表数据
	 * 
	 * @param entity
	 * @return
	 */
	@Override
	public List<Server> findList(Server server) {
		// 去中间表查询对应的tyepid
		ServerServertype serverServertype = new ServerServertype();
		List<Server> serverList = Lists.newArrayList();

		//根据服务器类型名和服务器名共同去查询
		if (StringUtils.isNotEmpty(server.getName()) && StringUtils.isNoneEmpty(server.getServertype())) {
			// 根据类型名去查类型id
			Servertype spe = servertypeService.getName(server.getServertype());
			// 根据服务器名查询类型id
			List<Server> srList = dao.findList(server);
			for (Server sr : srList) {
				ServerServertype sst = new ServerServertype();
				sst.setServerid(sr.getId());
				List<ServerServertype> sstList = serverServertypeService.findList(sst);
				for (ServerServertype st : sstList) {
					if (st.getTypeid().equals(spe.getId())) {
						serverList.add(sr);
					}
				}
			}
		} else 
			//根据服务器类型查询
			if (StringUtils.isNoneEmpty(server.getServertype())) {
			// 根据类型名去查类型id
			Servertype spe = servertypeService.getName(server.getServertype());
			//获得类型id  再根据类型id去查中间表
			ServerServertype sst = new ServerServertype();
			sst.setServerid(spe.getId());
			List<ServerServertype> sstList = serverServertypeService.findList(sst);
			for (ServerServertype st : sstList) {
				serverList.add(dao.get(st.getServerid()));
			}
		}else{
			//根据项目名称查询，或者获取所有数据集合
			
			// 根据服务器名查询类型id
			serverList = dao.findList(server);
		} 

		//拼接类型字符串
		for (Server sv : serverList) {
			serverServertype.setServerid(sv.getId());
			List<ServerServertype> serverServertypeList = serverServertypeService.findList(serverServertype);
			// 用于拼接服务器类型type
			String type = "";
			for (int i = 0; i < serverServertypeList.size(); i++) {
				// 根据重中间表获取的typeid查询服务器类型type
				Servertype servertype = servertypeService.get(serverServertypeList.get(i).getTypeid());
				// 拼接
				type += servertype.getType();
				// 最后一个不加逗号
				if (i != serverServertypeList.size() - 1) {
					type += ",";
				}
			}
			// 将type赋值给servertype
			sv.setServertype(type);
		}
		return serverList;
	}

	/**
	 * 查询分页数据
	 * 
	 * @param page
	 *            分页对象
	 * @param entity
	 * @return
	 */
	@Override
	public Page<Server> findPage(Page<Server> page, Server server) {
		server.setPage(page);
		page.setList(findList(server));
		return page;
	}

	/**
	 * 删除数据
	 * 
	 * @param id
	 */
	@Override
	@Transactional(readOnly = false)
	public void delete(String id) {
		serverServertypeService.deleteServerId(id);
		dao.delete(id);
	}

	/**
	 * 删除数据
	 * 
	 * @param entity
	 */
	@Override
	@Transactional(readOnly = false)
	public void delete(Server server) {
		serverServertypeService.deleteServerId(server.getId());
		dao.delete(server);
	}

}